<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
use Dcat\Admin\Widgets\Alert;
// use Dcat\Admin\Widgets\Form;
class TechnologyController extends Controller
{
    //换班表
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$step->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('电除尘器提效改造项目技术规范书')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('电除尘器提效改造项目技术规范书');
            $form->action('Technologystep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->width('100%')
                ->add('原电除尘器设计参数', function (StepForm $step) use ($form) {
                    $info = '<i class="fa fa-exclamation-circle"></i>原电除尘器设计参数';
                    $step->html(Alert::make($info)->info());
                    $step->text('projectName',trans('项目名称'))->required();
                    $step->text('a3n',trans('有效断面积'))->required();
                    $step->text('a3o',trans('电场气流速度'))->required();
                    $step->text('a1f',trans('处理烟气量'))->required();
                    $step->text('ccxl',trans('除尘效率'))->required();
                    $step->text('a1g',trans('许用温度'))->required();
                    $step->text('time',trans('烟气在电场内停留时间'))->required();
                    $step->text('a3f',trans('室数'))->required();
                    $step->text('a3e',trans('列数'))->required();
                    $step->text('a3g',trans('电场数'))->required();
                    $step->text('a3t',trans('同极间距'))->required();
                    $step->text('a3r',trans('每电场收尘极排数'))->required();
                    $step->text('a3v',trans('比集尘面积'))->required();
                    $step->text('a3d',trans('出口粉尘浓度'))->required();
                    $step->text('Cathodeline',trans('阴极线数量'))->required()->help('<i style="color:red">注意：单位为m</i>');
                    $info = '<i class="fa fa-exclamation-circle"></i>本体修复配件';
                    $step->html(Alert::make($info)->info());
                    $step->text('a3q',trans('电场有效长度'))->required()->help('<i style="color:red">注意：单位为m</i>');
                    $step->text('a3h',trans('电场有效高度'))->required()->help('<i style="color:red">注意：单位为m</i>');
                    $step->text('a3u',trans('总收尘面积'))->required()->help('<i style="color:red">注意：单位为m</i>');
                    $step->text('a3w',trans('收尘板型式'))->required()->help('<i style="color:red">注意：单位为m</i>');
                    $step->text('a3l',trans('各电场电源额定二次电压(KV)'),'left')->rules('required|min:1');
                    $step->text('a3ls',trans('各电场电源额定二次电流(A)'),'left')->rules('required|min:1');
                })
                ->add('供货清单',function(StepForm $step){
                    $info = '<i class="fa fa-exclamation-circle"></i>高压系统';
                    $step->html(Alert::make($info)->info());
                    $step->text('gydy',trans('高压电源数量'));
                    // $step->text('gydy',trans('高压电源数量'));
                    $info = '<i class="fa fa-exclamation-circle"></i>本体大修';
                    $step->html(Alert::make($info)->info());
                    $step->text('d2am',trans('灰斗数量'));
                    $step->text('yjb',trans('阳极板数量'));
                    $step->text('Cathodelines',trans('阴极线数量'));
                    $step->text('AnodeHammer',trans('阳极振打锤数量'));
                    $step->text('CathodeHammer',trans('阴极振打锤数量'));
                    $step->text('AnodeAnvil',trans('阳极振打砧数量'));
                    $step->text('CathodeAnvil',trans('阴极振打砧数量'));
                    $step->text('d1gm',trans('人孔门数量'));
                    $step->text('PorcelainVase',trans('瓷瓶数量'));
                    $step->text('PorcelainSpindle',trans('瓷转轴数量'));
                    $step->text('d1am',trans('贴板数量'));
                    
                    $info = '<i class="fa fa-exclamation-circle"></i>热风吹扫系统-大梁热风吹扫系统';
                    $step->html(Alert::make($info)->info());
                    $step->text('DalyanHotBlower',trans('大梁热风风机数量'))->required();
                    $step->text('DalyanHotHeater',trans('大梁热风加热器数量'))->required();
                    $step->text('DalyanHotPipeline',trans('大梁热风管路数量'))->required();
                    $step->text('PipelineValvesFittings1',trans('管道阀门配件数量'))->required();
                    // $step->text('InsulatorCover',trans('绝缘子盖板数量'))->required();
                    $step->text('InsulatorCover',trans('喷吹盖板数量'));
                    
                    $info = '<i class="fa fa-exclamation-circle"></i>热风吹扫系统-转轴箱热风吹扫系统';
                    $step->html(Alert::make($info)->info());
                    $step->text('SpindleBoxBlower',trans('转轴箱热风风机数量'))->required();
                    $step->text('SpindleBoxHeater',trans('转轴箱热风加热器数量'))->required();
                    $step->text('HotPipingSystem',trans('热风管路系统数量'))->required();
                    $step->text('PipelineValvesFittings2',trans('管道阀门配件数量'))->required();
                    $info = '<i class="fa fa-exclamation-circle"></i>随机备品备件清单';
                    $step->html(Alert::make($info)->info());
                    $step->text('sjCathodeline',trans('阴极线数量'))->required();
                    $step->text('sjyjzdc',trans('阳极振打锤数量'))->required();
                    $step->text('sjyjzdc1',trans('阴极振打锤数量'))->required();
                    $step->text('sjczz',trans('瓷转轴数量'))->required();

                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/jishuguifan.docx"));

            $templateProcessor->setValue('a3n',$info['a3n']);
            $templateProcessor->setValue('a3o',$info['a3o']);
            $templateProcessor->setValue('a1f',$info['a1f']);
            $templateProcessor->setValue('ccxl',$info['ccxl']);
            $templateProcessor->setValue('a1g',$info['a1g']);
            $templateProcessor->setValue('time',$info['time']);
            $templateProcessor->setValue('a3f',$info['a3f']);
            $templateProcessor->setValue('a3e',$info['a3e']);
            $templateProcessor->setValue('a3g',$info['a3g']);
            $templateProcessor->setValue('a3t',$info['a3t']);
            $templateProcessor->setValue('a3r',$info['a3r']);
            $templateProcessor->setValue('a3v',$info['a3v']);
            $templateProcessor->setValue('a3d',$info['a3d']);
            $templateProcessor->setValue('Cathodeline',$info['Cathodeline']);
            $templateProcessor->setValue('a3q',$info['a3q']);
            $templateProcessor->setValue('a3h',$info['a3h']);
            $templateProcessor->setValue('a3u',$info['a3u']);
            $templateProcessor->setValue('a3w',$info['a3w']);
            $templateProcessor->setValue('a3l',$info['a3l']);
            $templateProcessor->setValue('a3ls',$info['a3ls']);
            $templateProcessor->setValue('gydy',$info['gydy']);

            // $templateProcessor->setValue('',$info['']);

            $c5am = (isset($b['ganshi']['c5am']))?:0;
            $c5bm = (isset($b['ganshi']['c5bm']))?:0;
            $templateProcessor->setValue('yjb',$c5am+$c5bm); //阳极板
            $templateProcessor->setValue('ts',$info['a3e']*$info['a3f']); //套数
            $d1am = (isset($info['d1am']))?:0;
            $d1bm = (isset($info['d1bm']))?:0;
            $d2am = (isset($info['d2am']))?:0;
            $templateProcessor->setValue('hd',$d1am+$d1bm+$d2am); //灰斗、外壳进行漏风修补
            $templateProcessor->setValue('a3d',$info['a3d']); //出口粉尘浓度
            $templateProcessor->setValue('dy',$info['a3l']);//电压  
            $templateProcessor->setValue('dl',$info['a3ls']*1000);//电流 

            $templateProcessor->setValue('projectName',$info['projectName']);
            $templateProcessor->setValue('yearmonth',date('Y-m-d H:i:s'));

            $templateProcessor->setValue('gydy',$info['gydy']);
            $templateProcessor->setValue('d2am',$info['d2am']);
            $templateProcessor->setValue('yjb',$info['yjb']);
            $templateProcessor->setValue('Cathodelines',$info['Cathodelines']);
            $templateProcessor->setValue('AnodeHammer',$info['AnodeHammer']);
            $templateProcessor->setValue('CathodeHammer',$info['CathodeHammer']);
            // $templateProcessor->setValue('Cathodeline',$info['Cathodeline']);
            // $templateProcessor->setValue('BearingsDust',$info['BearingsDust']);
            $templateProcessor->setValue('AnodeAnvil',$info['AnodeAnvil']);
            $templateProcessor->setValue('CathodeAnvil',$info['CathodeAnvil']);
            $templateProcessor->setValue('d1gm',$info['d1gm']);
            $templateProcessor->setValue('PorcelainVase',$info['PorcelainVase']);
            // $templateProcessor->setValue('ColumnInsulators',$info['ColumnInsulators']);
            $templateProcessor->setValue('PorcelainSpindle',$info['PorcelainSpindle']);
            $templateProcessor->setValue('d1am',$info['d1am']);
            // $templateProcessor->setValue('Orificeplate',$info['Orificeplate']);
            $templateProcessor->setValue('DalyanHotBlower',$info['DalyanHotBlower']);
            $templateProcessor->setValue('DalyanHotHeater',$info['DalyanHotHeater']);
            $templateProcessor->setValue('DalyanHotPipeline',$info['DalyanHotPipeline']);
            $templateProcessor->setValue('PipelineValvesFittings1',$info['PipelineValvesFittings1']);
            $templateProcessor->setValue('InsulatorCover',$info['InsulatorCover']);

            $templateProcessor->setValue('SpindleBoxBlower',$info['SpindleBoxBlower']);
            // $templateProcessor->setValue('SpindleBoxHeatergg',$info['SpindleBoxHeatergg']);
            $templateProcessor->setValue('SpindleBoxHeater',$info['SpindleBoxHeater']);
            $templateProcessor->setValue('HotPipingSystem',$info['HotPipingSystem']);
            $templateProcessor->setValue('PipelineValvesFittings2',$info['PipelineValvesFittings2']);

            $templateProcessor->setValue('sjCathodeline',$info['sjCathodeline']);
            $templateProcessor->setValue('sjyjzdc',$info['sjyjzdc']);
            $templateProcessor->setValue('sjyjzdc1',$info['sjyjzdc1']);
            $templateProcessor->setValue('sjczz',$info['sjczz']);

            
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = $info['projectName'].'电除尘器提效改造项目技术规范书'.date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 15;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            // $templateProcessor->saveAs(public_path('filemodellist/'.$info['projectname'].md5(time()) . ".docx"));
            $templateProcessor->saveAs(public_path('filemodellist/'.$data['name'].".docx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
}